package com.handydata.antifraud.ws.dap;

import java.util.Date;

import javax.annotation.PostConstruct;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.handydata.antifraud.annotation.Comment;
import com.handydata.antifraud.common.constant.DataSourceCode;
import com.handydata.antifraud.common.exception.WsInvokeException;
import com.handydata.antifraud.to.dap.DianHuaBangThreeFactorsReqTO;
import com.handydata.antifraud.to.dap.DianHuaBangThreeFactorsResTO;
import com.handydata.antifraud.util.DateUtil;
import com.handydata.antifraud.util.encrypt.Md5Utils;
import com.handydata.antifraud.ws.WsInvocationService;

/**
 * 电话邦三要素验证
 *
 */
@Slf4j
@Service("dianHuaBangThreeFactorsWsService")
@Comment("电话邦三要素验证")
public class DianHuaBangThreeFactorsWsServiceImpl
		extends
		DataPlatformWsService<DianHuaBangThreeFactorsReqTO, DianHuaBangThreeFactorsResTO>
		implements
		WsInvocationService<DianHuaBangThreeFactorsReqTO, DianHuaBangThreeFactorsResTO> {

	@Override
	public DianHuaBangThreeFactorsResTO query(DianHuaBangThreeFactorsReqTO req)
			throws WsInvokeException {
		log.info("电话邦三要素验证请求参数 {}", req);
		DianHuaBangThreeFactorsResTO resTO = new DianHuaBangThreeFactorsResTO();
		if (StringUtils.isEmpty(req.getName())) {
			log.error("电话邦三要素验证参数校验 {}", "name为空");
			resTO.setResCode("1111");
			resTO.setResMsg("电话邦三要素验证接口name参数为空");
			return resTO;
		}
		if (StringUtils.isEmpty(req.getIdCard())) {
			log.error("电话邦三要素验证参数校验 {}", "idCard为空");
			resTO.setResCode("1111");
			resTO.setResMsg("电话邦三要素验证接口idCard参数为空");
			return resTO;
		}
		if (StringUtils.isEmpty(req.getPhoneNum())) {
			log.error("电话邦三要素验证参数校验 {}", "phone为空");
			resTO.setResCode("1111");
			resTO.setResMsg("电话邦三要素验证接口phone参数为空");
			return resTO;
		}
		this.dataType = DataSourceCode.DIAN_HUA_BANG_THREE_FACTORS.getCode();
		resTO = executeQuery(req);
		return resTO;
	}

	@Override
	protected String createQueryXml(DianHuaBangThreeFactorsReqTO req) {
		StringBuilder xml = new StringBuilder();
		xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
		xml.append("<subatm>");
		xml.append("<application>GwBiz.Req</application>");
		xml.append("<version>1.0.0</version>");
		xml.append("<sendTime>").append(DateUtil.now14()).append("</sendTime>");
		xml.append("<transCode>100092</transCode>");
		xml.append("<channelId>").append(encryInfo.getChannelId()).append("</channelId>");
		xml.append("<channelOrderId>").append(DateUtil.now17()).append("</channelOrderId>");
		xml.append("<phone>").append(Md5Utils.md5ToHexStr(req.getPhoneNum())).append("</phone>");
		xml.append("<name>").append(Md5Utils.md5ToHexStr(req.getName())).append("</name>");
		xml.append("<idCard>").append(Md5Utils.md5ToHexStr(req.getIdCard())).append("</idCard>");
		xml.append("</subatm>");
		log.info("电话邦三要素调用翰迪数据平台请求报文：{}", xml);
		req.setQueryXml(xml.toString());
		return xml.toString();
	}

	@Override
	protected DianHuaBangThreeFactorsResTO parseResultAndSave(String resultXml,
			String orderId, Date createDate) {
		log.info("调用电话邦三要素查询接口返回报文：{}", resultXml);
		DianHuaBangThreeFactorsResTO resTO = new DianHuaBangThreeFactorsResTO();
		try {
			// 解析xml，保存本地DB
			Document doc = DocumentHelper.parseText(resultXml);
			Element rootElt = doc.getRootElement(); // 获取根节点
			// String phone = rootElt.elementTextTrim("phone"); //
			// 拿到subatm节点下的子节点phone值
			// String name = rootElt.elementTextTrim("name"); //
			// 拿到subatm节点下的子节点name值
			// String idCard = rootElt.elementTextTrim("idCard"); //
			// 拿到subatm节点下的子节点idCard值
			String respCode = rootElt.elementTextTrim("respCode"); // 拿到subatm节点下的子节点respCode值
			String respDesc = rootElt.elementTextTrim("respDesc"); // 拿到subatm节点下的子节点respDesc值
			resTO.setRespCodeOfDianHuaBangThreeFactors(respCode);
			resTO.setResCode(respCode);
			resTO.setResMsg(respDesc);
		} catch (Exception e) {
			log.info("调用电话邦三要素查询接口返回报文解析异常：{}", e);
			resTO.setResCode("1111");
			resTO.setResMsg("调用电话邦三要素查询接口返回报文解析异常");
		}
		log.info("调用电话邦三要素查询接口返回结果：{}", JSONObject.toJSONString(resTO));
		return resTO;
	}

	@SuppressWarnings("unchecked")
	@PostConstruct
	@Override
	public void register() {
		registerToWsServiceFactory(this, DianHuaBangThreeFactorsReqTO.class);
	}

}
